<?php
/**
 * Created by IntelliJ IDEA.
 * User: wanglu
 * Date: 2017/7/19
 * Time: 14:13
 */
/**
 * Created by wanglu on 2017/7/19.
 */
class Login extends AdminBase
{


    const PWD_EXPIR_TIME = 1800;

    function Login() {
        parent::__construct();
        $this->load->service('user_service');
    }

    public function index() {
        $this->load->view('/admin/login');
    }


    public function _inittimeZone() {
        /*
        $this->load->model('setting_model');
        $setting = $this->setting_model->findSetting();
        if (!empty($setting)) {
            $this->session->set_userdata('timezone', $setting['timezone']);
        } else {
            $this->session->set_userdata('timezone', TimeUtil::TIME_ZONE_UTC);
        }*/
    }

    public function signIn() {
        $username = $this->input->post('username');
        $pwd = $this->input->post("pwd");
        $res = array();
        $adminUrl  = base_url('admin/');



        $user = $this->user_service->get_user_by_name($username);
        if (empty($user)) {
            $res['code'] = 201;
            $res['msg'] = "用户不存在";
            die(json_encode($res));
        } else {
            $upwd = $user['pwd'];
            if ($pwd == $upwd) {
                $this->session->set_userdata('user', $user);
                $this->session->set_userdata('loginname', $username);
                $this->session->set_userdata('logged_in', TRUE);
                $this->session->set_userdata('last_login_time', time());
                $this->session->set_userdata('last_login_ip', $this->input->ip_address());
                $this->session->set_userdata('role', $user['role']);
                // $this->_inittimeZone();
                // redirect(base_url('admin/'));
                $res['code'] = 200;
                $res['url'] = $adminUrl;
                die(json_encode($res));
            } else {
                $res['code'] = 202;
                $res['msg'] = "密码错误";
                die(json_encode($res));
            }
        }
    }

    public function password() {
        $this->load->view('/admin/password');
    }

    public function loginOut() {
        $this->session->unset_userdata('logged_in');
        $this->session->unset_userdata("loginname");
        $this->session->unset_userdata("last_login_time");
        $this->session->unset_userdata("last_login_ip");
        redirect(base_url('login/'));
    }



}